package org.example.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.security.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.example.config.SerialType;
import org.example.domain.*;
import org.example.enums.ContractTypeEnum;
import org.example.enums.FinanceApplyStateEnum;
import org.example.enums.FreezeStatusEnum;
import org.example.enums.SkStatusEnum;
import org.example.service.*;
import org.example.mapper.TJrFactoringMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
* @author 任众
* @description 针对表【t_jr_factoring(租约保理申请表)】的数据库操作Service实现
* @createDate 2023-05-08 15:25:57
*/
@Slf4j
@Service
public class TJrFactoringServiceImpl extends ServiceImpl<TJrFactoringMapper, TJrFactoring>
    implements TJrFactoringService{
    @Autowired
    TJrProductService tJrProductService;

    @Autowired
    TCfContractService contractService;

    @Autowired
    TJrFactoringDetailService tJrFactoringDetailService;


    @Autowired
    TCwReceiptPlanService tCwReceiptPlanService;
    @Override
    public void addTJrFactoring(TJrFactoring tJrFactoring) {

        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");String s = format.format(date);
        //租约保理申请单号赋值
        tJrFactoring.setFactoringOrderNo(SerialType.ZUYUEBAOLI+s+ IdUtil.getSnowflakeNextIdStr().substring(0,10));

        // 金融产品信息
        TJrProduct byId = tJrProductService.getById(tJrFactoring.getJrProductId());
        // 资方信息
        tJrFactoring.setCapitalId(byId.getCompanyId());
        //资方名称
        tJrFactoring.setCapitalName(byId.getCompanyName());
        // 借款金额 = 授信金额合计
        tJrFactoring.setLoanAmount(tJrFactoring.getFactoringCreditMoney());

        // 申请状态：申请中
        tJrFactoring.setApplyState(FinanceApplyStateEnum.SHZ.getIndex());
        Date nowTime = new Date();

        // 公共信息
        tJrFactoring.setDelFlg(1);
        tJrFactoring.setCreateTime(nowTime);
        tJrFactoring.setCreateUserid(SecurityUtils.getUserId());
        tJrFactoring.setUpdateTime(nowTime);
        tJrFactoring.setUpdateUserid(SecurityUtils.getUserId());
        this.save(tJrFactoring);



        // 保存保理明细
        List<TJrFactoringDetail> jrFactoringDetailList = tJrFactoringDetailService.list();
        if (jrFactoringDetailList != null) {
            for (TJrFactoringDetail jrFactoringDetailEntity : jrFactoringDetailList) {
                // 保理申请ID
                jrFactoringDetailEntity.setFactoringId(tJrFactoring.getId());
                jrFactoringDetailEntity.setBusinessId(Long.valueOf(tJrFactoring.getBusinessId()));
                // 公共信息
                jrFactoringDetailEntity.setDelFlg(1);
                jrFactoringDetailEntity.setCreateTime(nowTime);
                jrFactoringDetailEntity.setCreateUserid(SecurityUtils.getUserId());
                jrFactoringDetailEntity.setUpdateTime(nowTime);
                jrFactoringDetailEntity.setUpdateUserid(SecurityUtils.getUserId());
                //保理明细添加
                tJrFactoringDetailService.save(jrFactoringDetailEntity);


                // 冻结合同并且添加金融标识
                TCfContract contract = contractService.getById(jrFactoringDetailEntity.getCfContractId());
                // 已冻结
                contract.setFreezeStatus(FreezeStatusEnum.YDJ.getIndex());
                // 租约保理
                contract.setZyblFlg(0);
                contract.setUpdateTime(nowTime);
                contract.setUpdateUserid(SecurityUtils.getUserId());
                contractService.updateById(contract);


                // 更新出房合同上未收款的收款计划收款状态:未收款->冻结状态
                TCwReceiptPlan tCwReceiptPlan = new TCwReceiptPlan();
                tCwReceiptPlan.setContractId(jrFactoringDetailEntity.getCfContractId());
                tCwReceiptPlan.setContractType(ContractTypeEnum.CF.getIndex());
                tCwReceiptPlan.setSkStatus(SkStatusEnum.YDJ.getIndex());
                tCwReceiptPlanService.save(tCwReceiptPlan);
            }
            log.info("saveFactoringApply保存租约保理申请End...！");
        }
    }
}




